package app.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import app.Data.User;
import app.Logic.UserAccounts;

public class LogIn {
	private static HomeScreen outsideFrame;
	
	private static JFrame frame;
	private static UserAccounts accounts;
	
	private static JButton loginButton;
	private static JButton createAccountButton;
	private static JButton cancelButton;
	private static JTextField userName;
	private static JTextField password;
	
	private static LogInHandler logInButtonHandler;
	private static CreateAccountHandler createAccountButtonHandler;
	private static CancelHandler cancelButtonHandler;
	
	public LogIn(UserAccounts list, HomeScreen outside){
		outsideFrame = outside;
		
		frame = new JFrame();
		frame.setSize(350, 150);
		frame.setResizable(false);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		
		accounts = list;
		logInButtonHandler = new LogInHandler();
		createAccountButtonHandler = new CreateAccountHandler();
		cancelButtonHandler = new CancelHandler();
	}
	
	public void loginPanel(){
		JPanel innerPanel = new JPanel();
		innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));


		JPanel userN = new JPanel();
		JLabel nameLabel = new JLabel("User Name: ");
		userName = new JTextField(10);
		userN.add(nameLabel);
		userN.add(userName);
		innerPanel.add(userN);
		
		JPanel passwordN = new JPanel();
		JLabel passwordLabel = new JLabel("Password:   ");
		password = new JTextField(10);
		passwordN.add(passwordLabel);
		passwordN.add(password);
		innerPanel.add(passwordN);
		
		loginButton = new JButton("Log In");
		createAccountButton = new JButton("Create Account");
		cancelButton = new JButton("Cancel");
		
		loginButton.addActionListener(logInButtonHandler);
		createAccountButton.addActionListener(createAccountButtonHandler);
		cancelButton.addActionListener(cancelButtonHandler);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(loginButton);
		buttonPanel.add(createAccountButton);
		buttonPanel.add(cancelButton);
		innerPanel.add(buttonPanel);
		
		frame.add(innerPanel);
		frame.setVisible(true);
	}
	
	class LogInHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			
			String name = userName.getText();
			String pass = password.getText();
			
			if((name.equals("")) || (pass.equals(""))){
				JOptionPane.showMessageDialog(frame, "User name and/or password is not valid", 
						"Log In Error", 
						JOptionPane.ERROR_MESSAGE);
			}
			else if(!(accounts.checkUserName(name))){
				JOptionPane.showMessageDialog(frame, "Invalid user name.\nLog in is not successful.", 
						"Log In Error", 
						JOptionPane.ERROR_MESSAGE);
			}
			else if(!(accounts.authenticate(name, pass))){
				JOptionPane.showMessageDialog(frame, "Invalid Password.\nLog in is not successful.", 
						"Log In Error", 
						JOptionPane.ERROR_MESSAGE);
			}
			else{
				User u = accounts.getSingleAccount(name, pass);
				accounts.setActiveUser(u);
				
				JOptionPane.showMessageDialog(frame, 
						"You have been logged in the following account:\n" +
						"\nUserName: "+name+
						"\nPassword: "+pass, 
						"Log In", 
						JOptionPane.INFORMATION_MESSAGE);
				
				outsideFrame.loginGUI();
				frame.setVisible(false);
				frame.removeAll();
			}
		}
	}
	
	class CreateAccountHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			CreateAccount ca = new CreateAccount(accounts, outsideFrame);
			frame.setVisible(false);
			frame.removeAll();
			ca.createAccountPanel();
		}
	}
	
	class CancelHandler implements ActionListener{
		public void actionPerformed(ActionEvent e){
			frame.setVisible(false);
			frame.removeAll();
		}
	}
}
